﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Windows;
using OutlookHub.Utils;
using Silvermoon;
using OutlookHub.Styles;
using Silvermoon.Shapes;
using System.Drawing;
using Silvermoon.Images;

namespace OutlookHub.Pages
{
    public class NewCategoryPage : Page
    {
        private string category;
        private ToolbarButton okBtn;
        private RotateTransform rotation;
        private ScrollPanel panel;
        private Label label;
        private TextBox textBox;

        public NewCategoryPage()
            : base()
        {
            InitializePage();
        }

        public bool IsSaveEnabled
        {
            get { return okBtn.Enabled; }
            set { okBtn.Enabled = value; }
        }

        public string Category
        {
            get { return category; }
            set
            {
                category = value;
                textBox.Text = value;
            }
        }

        private void InitializePage()
        {
            Title = "NEW CATEGORY";
            Color = PhoneColors.Transparent;

            rotation = new RotateTransform { Axis = RotateAxis.XAxis, Angle = 0 };
            int h = 0;
            panel = new ScrollPanel
            {
                Margin = new Margin(0, h, 0, null),
                Height = 400,
                Color = PhoneColors.AlphaColor(PhoneColors.Dark, 250),
                Transformation = rotation,
                IsFocusable = false,
            };
            panel.TouchDown += (s, e) => e.FocusSet = true;
            Controls.Add(panel);

            label = new Label { Text = "Name:", AutoSize = true, TextColor = PhoneColors.Accent, Location = new Point(10, 10) };
            textBox = new TextBox { Margin = new Margin(10, 64, 10, null), Height = 64 * 3, AutoHeight = true, Font = FontPool.MediumRegularFont, AcceptReturn = true };
            textBox.Watermark = new StringShape { Text = "Enter category", Opacity = 160, Color = PhoneColors.Dark };
            textBox.KeyPress += new EventHandler<System.Windows.Forms.KeyPressEventArgs>(textBox_KeyPress);
            panel.Controls.Add(label, textBox);

            Toolbar tb = new Toolbar();
            Toolbar = tb;

            okBtn = tb.AddButton(SysImages.Ok);
            okBtn.Tap += OnOK;
            okBtn.Enabled = false;
            tb.AddButton(SysImages.Cancel, Page.BackCommand);
        }

        protected virtual void OnOK(object sender, EventArgs e)
        {
            category = textBox.Text;
            GoBack();
            if (OK != null) OK(this, EventArgs.Empty);
        }

        protected override void OnShown()
        {
            base.OnShown();
            textBox.PreLoad();
            textBox.Focus();
            textBox.SelectAll();
            textBox.TextChanged += new EventHandler(textBox_TextChanged);
            IsKeyboardVisible = true;
        }

        void textBox_TextChanged(object sender, EventArgs e)
        {
            IsSaveEnabled = !string.IsNullOrEmpty(textBox.Text);
        }

        void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == '\n')
            {
                e.Handled = true;
                OnOK(this, null);
            }
        }

        public event EventHandler OK;

    }
}
